package com.atguigu.team.service;

import com.atguigu.team.domain.Architect;
import com.atguigu.team.domain.Designer;
import com.atguigu.team.domain.Employee;
import com.atguigu.team.domain.Programmer;

/**
 * @author zhouzhou
 * @version 1.0
 * @description: TODO
 * @date 2025/2/15 21:48
 */

public class TeamService {
    private static int counter=1;
    private final static int MAX_EMMBER=5;//表示开发团队的最大成员数.
    private Programmer[] team=new Programmer[MAX_EMMBER];//当前团队的各成员对象.
    private int total;//记录成员的实际人数.

    public Programmer[] getTeam() {
        Programmer[] team=new Programmer[total];
        for(int i=0;i<total;i++){
            team[i]=this.team[i];
        }
        return team;
    }

    public void addMember(Employee e)throws TeamException {
        //成员已满
        if (total>=MAX_EMMBER){
            throw new TeamException("成员已满,无法添加");
        }
        //该成员不是开发人员,无法添加
        if (!(e instanceof Programmer p)){
            throw new TeamException("该成员不是开发人员,无法添加");
        }
        //该成员处于团队中或者处于假期.
        Status status = ((Programmer) e).getStatus();
        switch (status){
            case BUSY -> throw new TeamException("该成员已是团队成员");
            case VOCATION -> throw new TeamException("该成员正在休假,无法添加");
        }
        //该成员已在本开发团队中.
        boolean isExist=isExist(p);
        if (isExist){
            throw new TeamException("该成员已在本开发团队中");
        }

        int ProNUm=0,DesNum=0,ArcNum=0;
        for(int i=0;i<total;i++){
            if(team[i] instanceof Programmer){
                ProNUm++;
            } else if (team[i] instanceof Designer) {
                DesNum++;
            } else if (team[i] instanceof Architect) {
                ArcNum++;
            }
        }
        if (p instanceof Architect ){
            if (ArcNum>=2){
                throw new TeamException("团队中至多只能1名架构师");
            }
        }else if (p instanceof Designer){
             if (DesNum>=3){
                 throw new TeamException("团队中至多只能2名设计师");
             }
        }else if (p instanceof Programmer){
            if (ProNUm>=3){
                throw new TeamException("团队中至多只能3名程序员");
            }
        }
        //如果代码执行到此，代表可以add.
        team[total++]=p;
        p.setMemberld(counter++);
        p.setStatus(Status.BUSY);



    }

    private boolean isExist(Programmer p) {
        for (int i=0;i<total;i++){
            if (p.getId()==team[i].getId()){
                return true;
            }
        }
        return false;
    }

    public void removeMember(int memberId) throws TeamException {
            int i;
        for ( i=0;i<total;i++){
                if (team[i].getId()==memberId){
                    //找到了这个员工,需要修改该员工的属性.
                    team[i].setStatus(Status.FREE);
                    //员工的memberId可以不修改
                    break;
                }
            }
            //没找到
        if(i==total){
            throw new TeamException("没找到该员工");
        }
        //调整数组
        for (int j=i;j<total;j++){
            team[j]=team[j+1];
        }
        //total-1的位置先改成null，后再total减1
//        team[total-1]=null;
//        total--;
        team[--total]=null;// total-- ->null;

    }
}
